import env from '@/application/env/env.js'

export function loadSave(imgurl) {
	console.log("file", imgurl, env.baseUrl)
	return new Promise((resolve, reject) => {
		uni.request({
			url: env.baseUrl + imgurl,
			success: (res) => {
				console.log(res)
				uni.saveFile({
					tempFilePath: res.tempFilePath,
					success: (saveRes) => {
						resolve(saveRes)
					}
				})
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

export function saveImage(img) {
	return new Promise((resolve, reject) => {
		uni.saveFile({
			tempFilePath: img,
			success: (saveRes) => {
				resolve(saveRes)
			},
			fail: (err => {
				console.log(err)
				reject(err)
			})
		})
	})
}

export function downLoad(imgurl) {
	return new Promise((resolve, reject) => {
		uni.downloadFile({
			url: env.baseUrl + imgurl,
			success: (res) => {
				console.log(res)
				uni.saveFile({
					tempFilePath: res.tempFilePath,
					success: function(save) {
						resolve(save)
					},
					fail: (err) => {
						reject(save)
					}
				})
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

export function filesList() {
	return new Promise((resolve, reject) => {
		uni.getSavedFileList({
			success: function(res) {
				resolve(res.fileList)
			},
			fail: (err) => {
				reject(err)
			}
		});
	})

}
export function removeSaveFile(file){
	      uni.removeSavedFile({
	        filePath: file,
	        complete: function (res) {
	          console.log(res);
	        }
	      });
}

export function uploadImage(imgpath) {
	return new Promise((resolve, reject) => {
		const value = uni.getStorageSync('token')
		uni.uploadFile({
			url: env.baseUrl + '/file/uploadFile', //仅为示例，非真实的接口地址
			filePath: imgpath,
			name: 'file',
			header: {
				Authorization: "Bearer " + value
			},

			success: (uploadFileRes) => {
				resolve(uploadFileRes.data)
			},
			fail: (err) => {
				reject(err)
			}
		});
	})
}